package com.appkudos.view;

import com.appkudos.controller.GroupController;
import com.appkudos.controller.UserController;
import com.appkudos.model.App;
import com.appkudos.model.Group;
import com.appkudos.model.User;
import com.appkudos.view.R;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Browser;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter.ViewBinder;

public class GroupMemberActivity extends Activity implements ViewBinder{
    // list view for displaying the installed apps
	private ListView lv;
	private PackageManager pm;
	private TextView title_tv;
	private Activity PARENTACTIVITY;
	UserController uc;
	GroupController gc;
	User user;
	Group group;
	ArrayList<User> members;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PARENTACTIVITY = this;
        // set content view to apps_view xml
        setContentView(R.layout.view_title_list);   
        
        // get data from bundle
        Intent launchIntent = getIntent();
        Bundle extras = launchIntent.getExtras();
        uc = new UserController();
        gc = new GroupController();
        user = uc.getCurrentUser(extras);
        group = uc.getCurrentGroup(extras);
        
        // set the title of the page 
        title_tv=(TextView)findViewById(R.id.tl_title);
        title_tv.setText(group.getGroupName()+"'s Member List");
        
        // get list view from layout
        lv=(ListView)findViewById(R.id.tl_list);
		
     // get members for this group
  	    members = (ArrayList<User>) gc.getGroupMembers(group.getGroupID());
  	   
  	    // display members
  	    displayMatchedMembers(members);
  	  
		lv.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
		      User member = members.get(position);
		      // When a user is clicked, go to the user's profile
//		      Toast.makeText(getApplicationContext(), member.getUsername()+" is clicked",Toast.LENGTH_SHORT).show();
		     
		      Intent intent = new Intent(PARENTACTIVITY, AppKudos.class);
  		      intent = uc.passCurrentUser(intent, user);
  		      intent = uc.passFriend(intent, member);
  		      startActivity(intent);
		    }
		});
   }
    
    
    // helper method to display members
	private void displayMatchedMembers(List list){
	  	Map<String, User> itemData;
	  	User matched;
	  	List<Map<String, User>> listData = new ArrayList<Map<String, User>>();
	  	
	  	// put the friends into a hashmap (used for input by simple adapter)
	  	for(int j=0; j<list.size();j++){
	  		itemData = new HashMap<String, User>(1);
	  		matched = (User) list.get(j);
	  		itemData.put("single_list_item", matched);
	  		listData.add(itemData);
	  	}    
	
	  	// bind simple adapter to the list view
	      SimpleAdapter simpleAdapter = new SimpleAdapter(this, listData, R.layout.single_text_item,
	              new String[] {
	                  "single_list_item"
	              }, new int[] {
	                  R.id.single_list_item
	              });
	      
	      // bind viewBinder to adapter and set adapter to listview
	      simpleAdapter.setViewBinder(this);
	      lv.setAdapter(simpleAdapter);
	  }

	 @Override
	  public boolean setViewValue(View view, Object data, String stringRepresetation) {  	
		  	  // get data for the row
		  	  User matched = (User) data;
		      TextView tv = (TextView) view;
		      
		      // bind the matched user to textView
		      tv.setText(matched.getUsername());
		      tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.usericon1, 0, 0, 0);
		      return true;
	  }

}